package com.qsi.app.golf.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.qsi.app.golf.model.Golfer;
import com.qsi.app.golf.model.GolfManager;

public class GolferAdminFormController extends SimpleFormController {
	
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	private GolfManager manager;

	public ModelAndView onSubmit(Object command) throws ServletException {

		Golfer golfer = ((Golfer) command);
		String name = golfer.getFirstName() + golfer.getLastName();
		logger.debug("creating golfer " + name);
		manager.createGolfer(golfer);
		logger.debug("returning from GolferAdminForm view to "
				+ getSuccessView());
		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {

		Golfer golfer = new Golfer();
		golfer.setFirstName("Default");
		golfer.setLastName("Default");
		return golfer;
	}

	public void setGolfManager(GolfManager manager) {
		this.manager = manager;
	}

	public GolfManager getGolfManager() {
		return manager;
	}

}
